package com.zzyl.nursing.service.impl;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.dto.ReservationDto;
import com.zzyl.nursing.mapper.ReservationMapper;
import com.zzyl.nursing.service.IReservationService;
import com.zzyl.nursing.vo.CurrentReservationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.function.Consumer;

/**
 * 预约信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-12-13
 */
@Service
public class ReservationServiceImpl extends ServiceImpl<ReservationMapper, Reservation> implements IReservationService
{
    @Autowired
    private ReservationMapper reservationMapper;

    /**
     * 查询预约信息
     * 
     * @param id 预约信息主键
     * @return 预约信息
     */
    @Override
    public Reservation selectReservationById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询预约信息列表
     * 
     * @param reservation 预约信息
     * @return 预约信息
     */
    @Override
    public List<Reservation> selectReservationList(Reservation reservation)
    {
        return reservationMapper.selectReservationList(reservation);
    }

    /**
     * 新增预约信息
     * 
     * @param reservation 预约信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertReservation(Reservation reservation)
    {
        reservation.setStatus(0);
        reservation.setCreateBy(UserThreadLocal.get().toString());
        return save(reservation) ? 1 : 0;
    }

    /**
     * 修改预约信息
     * 
     * @param reservation 预约信息
     * @return 结果
     */
    @Override
    public int updateReservation(Reservation reservation)
    {
        return updateById(reservation) ? 1 : 0;
    }

    /**
     * 批量删除预约信息
     * 
     * @param ids 需要删除的预约信息主键
     * @return 结果
     */
    @Override
    public int deleteReservationByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除预约信息信息
     * 
     * @param id 预约信息主键
     * @return 结果
     */
    @Override
    public int deleteReservationById(Long id)
    {
        return removeById(id) ? 1 : 0;
    }

    @Override
    public Integer getTodayCancelCount() {
        // 获取今天的日期
        LocalDate today = LocalDate.now();
        // 获取0点0时0分的时间
        LocalTime midnight = LocalTime.of(0, 0, 0);
        // 组合日期和时间以获取今天的开始时间
        LocalDateTime startOfToday = LocalDateTime.of(today, midnight);
        LocalDateTime endOfToday = LocalDateTime.now();
        LambdaQueryWrapper<Reservation> between = Wrappers.<Reservation>lambdaQuery()
                .eq(Reservation::getStatus, 2)
                .between(Reservation::getUpdateTime, startOfToday, endOfToday);
        Long l = reservationMapper.selectCount(between);
        return l.intValue();
    }

    @Override
    public List<CurrentReservationVo> getTimeSoltReservationCount(Long time) {
        //Instant.ofEpochMilli(time)时间戳转时间, ZoneId.systemDefault()获取系统的时区
        LocalDate day = LocalDate.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault());
        System.out.println(day);
        // 格式化日期
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String dayString = day.format(formatter);
        // 拼接时间
        String startTime = dayString + " 00:00:00";
        String endTime = dayString + " 23:59:59";
        List<CurrentReservationVo> timeSoltReservationCount = reservationMapper.getTimeSoltReservationCount(startTime, endTime);
        return timeSoltReservationCount;
    }

    @Override
    public TableDataInfo pageQuery(ReservationDto dto) {
        IPage<Reservation> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        Long userId = UserThreadLocal.get();
        LambdaQueryWrapper<Reservation> queryWrapper = Wrappers.<Reservation>lambdaQuery()
                .eq(dto.getStatus() != null, Reservation::getStatus, dto.getStatus())
                .eq(Reservation::getCreateBy, userId);
        page = page(page, queryWrapper);
        return new TableDataInfo(page.getRecords(), (int) page.getTotal());
    }
    /**
     * 定时更新合预约信息状态
     */
    @Override
    public void updateReservationStatusJob() {
        //查询状态为0的,且预约时间大于当前时间的预约信息
        LambdaQueryWrapper<Reservation> queryWrapper = Wrappers.<Reservation>lambdaQuery()
                .eq(Reservation::getStatus, 0)
                .le(Reservation::getTime, LocalDateTime.now());
        List<Reservation> list = list(queryWrapper);
        list.forEach(reservation -> {
            reservation.setStatus(3);
            reservation.setUpdateTime(new Date());
        });
        //批量更新
        updateBatchById(list);
    }
}
